package com.briup.jz.web.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.briup.jz.bean.User;
import com.briup.jz.bean.extend.UserExtend;
import com.briup.jz.service.IUserService;
import com.briup.jz.utils.Message;
import com.briup.jz.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Api(description = "用户管理接口")
@RestController
@RequestMapping("/user")

public class UserController {
    @Autowired
    private IUserService userService;


    @ApiOperation(value = "获取全部")
    @GetMapping("find_all")
    public Message findAll() {
        List<User> list = userService.findAll();
        return MessageUtil.success(list);
    }


    @GetMapping("find_all_employee")
    public Message findAllEmployee(){
        return MessageUtil.success(userService.findAllEmployee());
    }

    @ApiOperation(value = "用户通过审核")
    @GetMapping("auditing")
    public Message auditing(long id){
        //userService是IUserService对象
        User user = userService.findById(id);
        user.setStatus("启用");
        userService.saveOrEdit(user);
        return MessageUtil.success("审核成功");
    }

    @ApiOperation(value = "拒绝用户通过审核")
    @GetMapping("refuse_auditing")
    public Message refuseauditing(long id){
        User user =  userService.findById(id);
        user.setStatus("禁用");
        userService.saveOrEdit(user);
        return MessageUtil.success("已拒绝审核");
    }

    @ApiOperation(value = "获取全部+角色信息")
    @GetMapping("find_all_with_role")
    public Message findAllWithRole() {
        List<UserExtend> list = userService.findAllWithRole();
        return MessageUtil.success(list);
    }

    @ApiOperation(value = "添加或通过ID修改")
    @ApiImplicitParam(name = "id" ,value = "用户唯一编号（添加新用户时不填）", paramType = "query")
    @PostMapping("save_or_edit")
    public Message saveOrUpdate(User user) {
        userService.saveOrEdit(user);
        return MessageUtil.success("SUCCESS");
    }

    @ApiOperation(value = "通过ID删除")
    @ApiImplicitParam(name = "id" ,value = "用户唯一编号", required = true, paramType = "query")
    @GetMapping("del_by_id")
    public Message delById(long id) {
        userService.delById(id);
        return MessageUtil.success("SUCCESS");
    }

    @ApiOperation(value = "通过ID查找")
    @ApiImplicitParam(name = "id" ,value = "用户唯一编号", required = true, paramType = "query")
    @GetMapping("find_by_id")
    public Message findById(long id) {
        UserExtend userExtend = userService.findById(id);
        return MessageUtil.success(userExtend);
    }

}
